package com.webreport.service.impl;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.webreport.dao.GenericDao;
import com.webreport.exception.DaoException;
import com.webreport.exception.ManagerException;
import com.webreport.service.GenericManager;

public class GenericManagerImpl<T, PK extends Serializable> implements GenericManager<T, PK> {
	protected final Log log = LogFactory.getLog(getClass());
	protected GenericDao<T, PK> dao;
	
	public GenericManagerImpl() {
	}
	
	public GenericManagerImpl(GenericDao<T, PK> genericDao) {
		this.dao = genericDao;
	}
	
	public List<T> getAll() {
		return this.dao.getAll();
	}
	
	public T get(PK id) {
		return this.dao.get(id);
	}
	
	public boolean exists(PK id) {
		return this.dao.exists(id);
	}
	
	public PK insert(T object) throws ManagerException {
		try {
			return this.dao.insert(object);
		} catch (DaoException e) {
			log.error("插入数据失败." + e.getMessage());
			throw new ManagerException(e.getMessage());
		}
	}
	
	public T update(T object) throws ManagerException {
		try {
			return this.dao.update(object);
		} catch (DaoException e) {
			log.error("更新数据失败." + e.getMessage());
			throw new ManagerException(e.getMessage());
		}
	}
	
}
